import Vue from 'vue'
import App from './App.vue'
import router from '@/router/index'
import 'amfe-flexible/index'
import store from '@/store'
// import { getItem } from '@/utils'

import {
  Button, Form, Field, Toast, Swipe, SwipeItem, Lazyload,
  Tab, Tabs, List, SwipeCell, Stepper, Checkbox, CheckboxGroup,
  Radio, RadioGroup
} from 'vant'

Vue.use(Button).use(Form).use(Field).use(Toast).use(Swipe).use(SwipeItem)
.use(Lazyload).use(Tabs).use(Tab).use(List).use(SwipeCell).use(Stepper)
.use(CheckboxGroup).use(Checkbox).use(Radio).use(RadioGroup)

Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  const isLogin = store.getters['user/getUserInfo'] !== null
  if (to.meta.login) {
    if (isLogin) {
      next()
    } else {
      store.dispatch('user/getUserInfo').then(res => {
        if (res && res.loginName) {
          next()
        } else {
          next({path: '/login'})
        }
      }).catch(err => {
        console.log(err)
      })
    }
  } else {
    if (to.name === 'Login' && isLogin) {
      next({path: '/home'})
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
